﻿using Crm.CrmClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public partial class Parametreler : ChildWindow
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        List<GenelTanim> tanimlar = new List<GenelTanim>();
        public Parametreler()
        {
            InitializeComponent();
            grdListe.DataSource = tanimlar;
          
            client.TanimListeleCompleted += client_TanimListeleCompleted;
            client.TanimEkleCompleted += client_TanimEkleCompleted;
            this.Loaded += Parametreler_Loaded;
        }

        void Parametreler_Loaded(object sender, RoutedEventArgs e)
        {
            client.TanimListeleAsync(100);
        }

        void client_TanimEkleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else
            {
                this.DialogResult = true;
            }
        }

        void client_TanimListeleCompleted(object sender, TanimListeleCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                tanimlar.AddRange(e.Result);
            }
            else
            {
                tanimlar.Add(new GenelTanim() { Adi = "Sonraki ziyaret günü kaç gün sonra", Deger = "30", Tip = 100 });
            }

            grdListe.ColumnsAutoWidth = true;
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["Tip"].Visible = false;
            grdListe.Columns["Deger"].AllowEditing = DevExpress.Utils.DefaultBoolean.True;
            grdListe.Refresh();
           
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < grdListe.RowCount; i++)
            {
                GenelTanim tnm = (GenelTanim)grdListe.GetDataRow(i);
                if (tnm != null)
                {
                    client.TanimEkleAsync(tnm);
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

